<template>
	<view class="content">
		<view class="uni-form-item uni-column">
			<view class="title">用户名</view>
			<input v-model="username" class="uni-input" focus placeholder="用户名" />
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">密码</view>
			<input v-model="password" class="uni-input" focus placeholder="密码" />
		</view>
		<view style="margin-top: 30px;">
			<button type="primary" @click="handLogin">登录</button>
			<button type="primary" @click="handPhoneLogin">手机号登录</button>
		</view>
		<view style="margin-top: 30px;">
			<button type="primary" @click="handRegister">没有账号点击去往注册</button>
			<button type="primary" @click="handfindpassword">找回密码</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '用户登录',
				username:'',
				password:'',
			}
		},
		onLoad() {
		},
		methods: {
			handPhoneLogin(){
				uni.navigateTo({
					url:"login1"
				})
			},
			handfindpassword(){
				uni.navigateTo({
					url:"../findpassword/index"
				})
			},
			handRegister(){
				uni.navigateTo({
					url:'../register/index'
				})
			},
			handLogin(){
				console.log(this.username,this.password)
				uni.request({
				    url: 'http://localhost:7000/user/loginusername', //接口地址。
				    data: {
				        username: this.username,
						password: this.password
				    },
				    header: {
				        'custom-header': 'hello' //自定义请求头信息
				    },
					method:'POST',
				    success: (res) => {
				        console.log(res.data);
						//把参数存到这里
						if(res.data.flag){
						uni.setStorage({
							key: 'userId',
							data: res.data.data.userId
						})
						uni.setStorage({
							key: 'userName',
							data: res.data.data.userName
						})
						uni.setStorage({
							key: 'userRole',
							data: res.data.data.userRole
						})
						//取参数在这里
						// uni.getStorage({
						//     key: 'userId',
						//     success: function (res) {
						//         console.log(res.data);
						//     }
						// });
						uni.showToast({
							title: "登录成功",
							icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
							duration: 2000    //持续时间为 2秒
						}),
						setTimeout(function(){
							uni.navigateTo({
								url:'../personal/index'
							})
						},3000)
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
								duration: 2000    //持续时间为 2秒
							})
						}
				    }
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>
